package movee.grpc.client;

import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;
import lombok.extern.slf4j.Slf4j;
import movee.protobuf.lib.greeter.GreeterGrpc;
import movee.protobuf.lib.greeter.HelloRequest;
import movee.protobuf.lib.greeter.HelloResponse;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

@Slf4j
@SpringBootApplication
public class GrpcClientApplication {

    public static void main(String[] args) {
        SpringApplication.run(GrpcClientApplication.class, args);
    }

    @Bean
    public CommandLineRunner runner() {
        return (args) -> {
            ManagedChannel channel = ManagedChannelBuilder
                    // server的地址
                    .forAddress("localhost", 8181)
                    .usePlaintext().build();

            // 创建一个存根
            GreeterGrpc.GreeterBlockingStub stub = GreeterGrpc.newBlockingStub(channel);

            // 通过存根调用远程服务
            HelloResponse response = stub.hello(HelloRequest.newBuilder()
                    .setName("movee")
                    .setAge(17)
                    .addHobbies("climbing")
                    .putTags( "xx","ok" )
                    .build());

            log.info("response: {}", response);
        };
    }
}
